<?php


namespace App\Repository\Repositories;


use App\Helpers\Code;
use App\Helpers\Token;
use App\Http\Controllers\Controller;
use App\Http\Serve\Es;
use App\Models\StoreHead;
use App\Repository\RepositoryInterface\HeadsInterface;

class HeadsRepositories extends Controller implements HeadsInterface
{
    protected $head;

    public function __construct(StoreHead $storeHead)
    {
        $this->head = $storeHead;
    }

    /**
     * 团长申请
     * @param $params
     * @return array|mixed
     */
    public function applyHead($params)
    {
        try {
            $uid = Token::verifyToken($params['token']);
            $data = [
                'uid'=>$uid,
                'avatar'=>$params['avatar'],
                'name'=>$params['name'],
                'phone'=>$params['phone'],
                'city_id'=>$params['city_id'],
                'address'=>$params['address'],
                'status'=>0,
            ];
            $res = $this->head::createHead($data);
            return parent::success("提交成功",$data);
        } catch (\Exception $exception){
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 团长列表查询
     * @param $param
     * @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function headList($params)
    {
        try {
            if (!empty($params)){
                $res = $this->head::search($params['start_at'],$params['end_at'],$params['keyword'],$params['status']);
                return ['code'=>20000,'msg'=>'查询成功','data'=>$res];
            }
            $data = $this->head->headList();
            return parent::success("查询成功",$data);
        }catch (\Exception $exception){
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 团长状态列表切换
     * @param $status
     * @return array|mixed
     */
    public function headStatus($status)
    {
        try {
            $data = $this->head::headStatus($status);
            return parent::success("查询成功",$data);
        }catch (\Exception $exception){
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 通过/不通过
     * @param $params
     * @return array|mixed
     */
    public function adopt($params)
    {
        try {
            $item = $this->head::headFind($params['id']);
            $res = $this->head::adopt($params['id'],$params['status']);
            $params['status']==1?$status="成功":$status="不通过";
            $sendMsg = (new Code())->getCode($item->phone,"请求已审核","wufen","04945e24cb434763a6053ac369bd42d8");
            if ($sendMsg=="短信发送成功"){
                return parent::success("审核成功",$res);
            }
            return parent::error("短信发送失败");
        }catch (\Exception $exception){
            return parent::error($exception->getMessage());
        }
    }

   /**
     * 团长管理列表
     * @param $params
     * @return array
     */
    public function adminHeadList($params){
        try {
            if (!empty($params)){
                $res = $this->head::search($params['start_at'],$params['end_at'],$params['keyword'],$params['status']);
                return parent::success("查询成功",$res);
            }
            $data = $this->head->headList()->toArray();
            foreach ($data['data'] as $v){
                (new Es())->add_doc($v['id'],$v,'head_doc','head_list');
            }
            return ['code'=>20000,'msg'=>'查询成功','data'=>$data];
        }catch (\Exception $exception){
            return parent::error($exception->getMessage());
        }
    }
/**
     * 团长管理列表es搜索
     * @param $params
     * @return \Illuminate\Http\JsonResponse
     */
    public function search($params){
        if (!empty($params)){
            $body = [
                'query' => [
                    'match' => [
                        'name' => [
                            'query' => $params
                        ]
                    ]
                ],
                'highlight'=>[
                    'fields'=>[
                        'name'=>[
                            'pre_tags'=>[
                                '<span style="color: red">'
                            ],
                            'post_tags'=>[
                                '</span>'
                            ]
                        ]
                    ]
                ]
            ];
            //调用es的搜索
            $res = (new Es())->search_doc('head_doc','head_list',$body);
            $data = array_column($res['hits']['hits'], '_source');
            foreach ($data as $key=>&$v){
                $v['title'] = $res['hits']['hits'][$key]['highlight']['name'][0];
            }
            unset($v);
            return parent::success("查询成功",$data);
        }
    }

    /**
     * 团长修改
     * @param $params
     * @return array
     */
    public function adminUpdateHead($params){
        try {
            $res = $this->head::updateHead($params);
            return parent::success("修改成功",$res);
        }catch (\Exception $exception){
            return parent::error($exception->getMessage());
        }
    }



}
